package model;

import org.apache.commons.collections.OrderedMap;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.PriceUtils;


import java.util.*;

public class Order {
    private int id;
    private float total;
    private int amount;
    private int status;
    private int paytype;
    private String name;
    private String phone;
    private String address;
    private Date datetime;
    private User user;
    private Map<Integer, OrderItem> itemMap=new HashMap<Integer, OrderItem>();
    private List<OrderItem> itemList=new ArrayList<OrderItem>();
    public void setUsername(String username){
        user=new User();
        user.setAccount(username);
    }
    public  void addGoods(Goods g){
        if(itemMap.containsKey(g.getId())){
            OrderItem item =itemMap.get(g.getId());
            item.setAmount(item.getAmount()+1);
        }else {
            OrderItem item =new OrderItem(g.getPrice(),1,g,this);
            itemMap.put(g.getId(),item);
        }
        amount++;
        total= PriceUtils.add(total,g.getPrice());
    }

    public List<OrderItem> getItemList() {return itemList;}
    public void setItemList(List<OrderItem> itemList) {this.itemList = itemList;}
    public void lessen(int goodsid){
        if(itemMap.containsKey(goodsid)){
            OrderItem item =itemMap.get(goodsid);
            item.setAmount(item.getAmount()-1);
            amount--;
            total=PriceUtils.subtract(total,item.getPrice());
            if(item.getAmount()<=0){
                itemMap.remove(goodsid);
            }
        }
    }
    public void delete(int goodsid) {
        if (itemMap.containsKey(goodsid)) {
            OrderItem item = itemMap.get(goodsid);
            total = PriceUtils.subtract(total, item.getPrice() * item.getAmount());
            amount -= item.getAmount();
            itemMap.remove(goodsid);
        }
    }

    public Order() {super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public float getTotal() {
        return total;
    }

    public void setTotal(float total) {
        this.total = total;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getPaytype() {
        return paytype;
    }

    public void setPaytype(int paytype) {
        this.paytype = paytype;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map<Integer, OrderItem> getItemMap() {
        return itemMap;
    }

    public void setItemMap(Map<Integer, OrderItem> itemMap) {
        this.itemMap = itemMap;
    }

    public Order(int id, float total, int amount, int status, int paytype, String name, String phone, String address, Date datetime, User user, Map<Integer, OrderItem> itemMap, List<OrderItem> itemList) {
        this.id = id;
        this.total = total;
        this.amount = amount;
        this.status = status;
        this.paytype = paytype;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.datetime = datetime;
        this.user = user;
        this.itemMap = itemMap;
        this.itemList = itemList;
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", total=" + total +
                ", amount=" + amount +
                ", status=" + status +
                ", paytype=" + paytype +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", datetime=" + datetime +
                ", user=" + user +
                ", itemMap=" + itemMap +
                ", itemList=" + itemList +
                '}';
    }
}
